/**
 * 使用 : Tips.init({parentDom:"",tipsDom:"",triangle:""});
 * 
 * @param $
 * @param w
 */
(function($, w) {

    if (!$ || !w)
		return;

	var Tips = function() {
	};
	Tips.prototype = {
		option : {},
		init   : function(opt) {
			this.initOption(opt).initPageTips();
		},
		initOption : function(opt) {
			this.option = {
				"parentDom": opt.parentDom || ".tips-parent",// tips的父级元素
				"tipsDom"  : opt.tipsDom   || ".dom-tips",   // 初始化的DOM
				"triangle" : opt.triangle  || "left"         // 位置
			};
			console.log(this.option);
			return this;
		},

		initPageTips : function() {
			var _this = this, _dom_tips = $(_this.option.tipsDom), _i = 0,_tips_length = _dom_tips.length ;
			if (_tips_length > 0) {
				for (_i; _i < _tips_length; _i++) {
					console.log(_dom_tips[_i]);
					var _dom_tips_dom = $(_dom_tips[_i]),
					 	_dom_tips_dom_id = _dom_tips_dom.attr("id"),
					 	_target = $("#"+_dom_tips_dom_id);
					
					console.log(_target);
					//设置目标属性
					_target.attr("target",_i);
					//鼠标事件
					$(_this.option.parentDom).on("mouseover", _this.option.tipsDom ,function() {
						_this.mouseoverProcess($(this));
					}).on("mouseout", _this.option.tipsDom , function() {
						_this.mouseoutProcess($(this));
					});
				}
			}
		},
		mouseoverProcess:function(eventThis){
			var _this = this,
				_event_this  = eventThis,
				
				_dom_target  = _event_this.attr("target"),
				_show_target = $("[show-target=" +_dom_target+ "]");
				
				//是否已经存在Tips的HTML
				if (_show_target.length == 0) {
					//获取对应的内容用于生成Tips
					var _tip_info = _event_this.attr("title"),
						_tipHTML  = _this.generateTips(_dom_target, _tip_info);
						//移除Title避免鼠标悬停显示
						_event_this.removeAttr("title");
						//输出HTML
						$("[target="+_dom_target+"]").append(_tipHTML);
				}
				//设置偏移坐标
				_show_target.offset(_this.tipsOffSet(_event_this)).show();
				//当前tips的坐标
				console.log(_show_target.offset());
				//三角形位置
				var _dom_tips_triangle = $("[show-target=" +_event_this.attr("target")+ "]").find(".dom-tips-triangle");
					_dom_tips_triangle.offset(_this.tipsTriangleOffSet(_event_this));
		},
		tipsOffSet : function(_event_this){
			console.log(_event_this);
			var _option_triangle = this.option.triangle,
				_target_width = _event_this.width(),
				_padding_left = Number(_event_this.css("padding-left").replace("px","")),
				_offset = _event_this.offset();
				
				console.log(_target_width);	
				console.log(_padding_left);	
				console.log(_offset);		
				if (_option_triangle === "left") {
					return {top:_offset.top - 5,left:_offset.left + _padding_left + _target_width + 10};
				} else if (_option_triangle === "right") {
					//todo
				}
		},
		mouseoutProcess:function(_this){
			$("[show-target=" +  _this.attr("target") + "]").hide();
		},
		generateTips : function(target, tip_info) {
			// 创建Tips的HTML
			var _tipHTML = [ '<span class="dom-tips-content"  show-target="',
			                 target,'"><span class="dom-tips-triangle ',
			                 this.triangleClass(), '"></span>', tip_info, '</span>'].join("");
			return _tipHTML;
		},
		tipsTriangleOffSet : function(_event_this){
			var _option_triangle = this.option.triangle,
				_target_width = _event_this.width(),
				_padding_left = Number(_event_this.css("padding-left").replace("px","")),
				_offset = _event_this.offset();
				
			if (_option_triangle === "left") {
				return {top:_offset.top + 4 ,left:_offset.left + _padding_left + _target_width + 1};
			} else if (_option_triangle === "right") {
				//todo
			}
		},
		triangleClass : function() {
			var _option_triangle = this.option.triangle;
			if (_option_triangle === "left") {
				return "left-triangle"
			} else if (_option_triangle === "right") {
				return "right-triangle"
			}
		}
	};
	w.Tips = new Tips();
})(jQuery, window);